package es.ats.codington.web.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.exceptions.ServiceException;
import es.ats.codington.festival.services.EventService;
import es.ats.codington.festival.services.VisitorService;

/**
 * Servlet implementation class UpdateEventServlet
 */
@WebServlet("/UpdateEventServlet")
public class UpdateEventServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateEventServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	
	/**
	 * Method invoked when an event is updated
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();  
		int idEvent = 0;
        
		HttpSession session = request.getSession(true);
		
		/**
		 * Parameters obtained form
		 */
		String e = request.getParameter("eventname");
		String ed = request.getParameter("eventdescription");
		String p = request.getParameter("places");
		String d1 = request.getParameter("date1");
		String du1 = request.getParameter("duration1");
		String du2 = request.getParameter("duration2");
		String et = request.getParameter("eventype");
		String a = request.getParameter("availabletickets");
		
	
		
		int aN = Integer.parseInt(a);
		int pN = Integer.parseInt(p);
		/**
		 * Get the id of event
		 */
		idEvent = (Integer) session.getAttribute("idEventAdminSession");
		
		EventService service = new EventService();
	    
		Event event = new Event();
	     
		event.setName(e);
		event.setDescription(ed);
		event.setPlace("");
		event.setDayEvent(d1);
		event.setStartEvent(du1);
		event.setEndEvent(du2);
		event.setEventType(et);
		event.setSeatsAvailable(aN); 
		event.setIdPlace(pN);
		event.setEventID(idEvent);
		
	    
	    try {
	    	/**
	    	 * The event is updated
	    	 */
	    	service.updateEvent(event);
	    	 /**
			 * Invokes the action "adminvisitor.jsp"
			 */
	    	//request.getRequestDispatcher("adminvisitor.jsp").forward(request, response);
	    	response.sendRedirect("ShowEventsAdminServlet");
	    	
		} catch (ServiceException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		} 
	    out.close(); 
	}

}
